package day02;

import util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestBatch {


    public static void main(String[] args) {


        long start=System.currentTimeMillis();

        Connection con=null;
        PreparedStatement ps=null;

        try{

            con=JdbcUtil.getConnection();

            con.setAutoCommit(false);


            String sql=new StringBuffer()
                    .append("insert into t_user ")
                    .append("    (name) ")
                    .append("values ")
                    .append("    (?) ")
                    .toString();

            ps=con.prepareStatement(sql);

            for (int i = 0; i < 30000; i++) {
                ps.setString(1,"b"+i);
                // ps.executeUpdate();//9573

                ps.addBatch();

                if(i%10000==0){
                    ps.executeBatch();//6903
                }

            }
            ps.executeBatch();

            con.commit();

        }catch (Exception e){
            e.printStackTrace();

            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }finally{
            JdbcUtil.close(con,ps,null);
        }

        long end=System.currentTimeMillis();

        System.out.println(end-start);

    }

}
